package com.shawn.baseappbygroovydsl.ui.views

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.graphics.Color
import android.graphics.Path
import android.graphics.drawable.GradientDrawable
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.animation.LinearInterpolator
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.choryan.quan.videowzproject.extension.ExtensionView.animationBreath
import com.shawn.baseappbygroovydsl.R
import com.shawn.baseappbygroovydsl.databinding.FragmentAnimatorBinding
import com.shawn.baseappbygroovydsl.ui.base.FragmentBase
import java.util.Random

class AnimatorFragment : FragmentBase<FragmentAnimatorBinding>(R.layout.fragment_animator) {
    private lateinit var slideshowViewModel: ViewModel
    private var x = 0f
    private val alphaAnimator by lazy {
        val fadeIn = ObjectAnimator.ofFloat(mBind.btnAlpha, "alpha", 0f, 1f)
        fadeIn.addListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator) {}

            override fun onAnimationEnd(animation: Animator) {}

            override fun onAnimationCancel(animation: Animator) {}

            override fun onAnimationRepeat(animation: Animator) {}

        })
        fadeIn.duration = 400L
        fadeIn
    }

    private val translationY by lazy {
        val duration = 300L
        val slideDown = ObjectAnimator.ofFloat(
            mBind.tvTranslationY,
            "translationY",
            -mBind.tvTranslationY.height.toFloat(),
            0f
        )
        val fadeIn = ObjectAnimator.ofFloat(mBind.tvTranslationY, "alpha", 0f, 1f)
        fadeIn.duration = duration
        slideDown.duration = duration
        val animatorSet = AnimatorSet()
        animatorSet.playTogether(slideDown, fadeIn)
        animatorSet
    }

    private val gradientAnimator by lazy {
        val value = ValueAnimator.ofFloat(0f, 1.1f)
        value.addUpdateListener {
            x = it.animatedValue as Float
            setGradientBg()
        }
        value.duration = 2000
        value
    }

    private fun startBubbleAnimation(vararg views: View) {
        val random = Random()
        views.forEach { v ->
            v.post {
                val index = random.nextInt(2)
                val path = Path()
                path.addCircle(
                    v.x, v.y, 15f + random.nextInt(6), if (index == 0) {
                        Path.Direction.CW
                    } else {
                        Path.Direction.CCW
                    }
                )
                val animator = ObjectAnimator.ofFloat(v, "x", "y", path)
                animator.repeatMode = ValueAnimator.RESTART
                animator.repeatCount = ValueAnimator.INFINITE
                animator.interpolator = LinearInterpolator()
                animator.duration = 3000L + random.nextInt(3) * 100L
                animator.start()
            }
        }
    }

    override fun statusBar() {

    }

    override fun lazyLoad() {
        slideshowViewModel = ViewModelProvider(this)[NestedViewModel::class.java]
        initView()
    }

    override fun onKeyCodeBack(): Boolean {
        return false
    }

    override fun initClick() {

    }

    private fun initView() {
        btnBreath()
        alpha()
        animator1()
        gradientBg()
        startPendulumAnimation(mBind.ivGift)
        startBubbleAnimation(mBind.ivBubble)
    }

    private fun btnBreath() {
        mBind.tvWithdrawConfirm.animationBreath()
    }

    private fun animator1() {
        mBind.btnTranslationY.setOnClickListener {
            translationY.start()
        }
    }

    private fun setGradientBg() {
        val colors = intArrayOf(
            Color.parseColor("#9932CC"),
            Color.parseColor("#FF69B4")
        )
        val offsets = floatArrayOf(
            x - 0.1f,
            x
        )
        val drawable = GradientDrawable()
        drawable.gradientType = GradientDrawable.LINEAR_GRADIENT
        drawable.orientation = GradientDrawable.Orientation.LEFT_RIGHT
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            drawable.setColors(colors, offsets)
        }
        mBind.tvGradient.background = drawable
    }

    private fun gradientBg() {
        setGradientBg()
        mBind.tvGradient.setOnClickListener {
            gradientAnimator.start()
        }
    }

    /**
     * 左右摇摆的动画
     * */
    fun startPendulumAnimation(view: View) {
        // 确保View已完成布局
        view.post {
            // 1. 设置旋转中心点为View的中心点
            view.pivotX = view.width / 2f
            view.pivotY = view.height / 2f

            // 2. 创建旋转动画（钟摆效果）
            val animator = ObjectAnimator.ofFloat(
                view,
                "rotation",  // 旋转属性
                -15f, 15f, -15f  // 左右摆动角度（单位：度）
            ).apply {
                duration = 700  // 动画时长（毫秒）
                repeatCount = ObjectAnimator.INFINITE  // 无限循环
                interpolator = LinearInterpolator()  // 线性插值器使摆动更自然
                repeatMode = ObjectAnimator.REVERSE  // 往返模式
            }
            animator.start()
        }
    }

    private fun alpha() {
        mBind.btn1.setOnClickListener {
            alphaAnimator.start()
        }
    }

    override fun getData() {

    }

    override fun getBind(view: View): FragmentAnimatorBinding {
        return FragmentAnimatorBinding.bind(view)
    }
    companion object {

        fun newInstance(): AnimatorFragment {
            val args = Bundle()
            val fragment = AnimatorFragment()
            fragment.arguments = args
            return fragment
        }
    }
}